package com.example.waterdispenserm2.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.waterdispenserm2.dp.M2NoodleHelperUtil;


public class SPUtils {


    private static Context context;

    /*
     * 描 述: 获取全局上下文
     */
    public static Context getContext() {
        if (context != null) return context;
        throw new NullPointerException("请前往LPHYApplication中初始化工具类");
    }
    /**
     * 初始化ToastUtils，建议在Application中初始化
     *
     */
    public static void init(Context context) {
        MyLog.setContext(context);
        SPUtils.context = context.getApplicationContext();
        M2NoodleHelperUtil.getInstance().init();
    }

    //创建
    public static SharedPreferences share(){
        SharedPreferences sharedPreferences = SPUtils.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        return sharedPreferences;
    }
    //清除
    public static void clear() {
        SharedPreferences.Editor editor = share().edit();
        editor.clear();
        editor.commit();
    }
    //储存
    private SharedPreferences.Editor setData( String keys,String values){
        SharedPreferences.Editor e = share().edit();
        e.putString(keys,values);
        return e;
    }

    public static String getSerialPortNum(){
        return share().getString("SerialPort","");
    }
    public static void setSerialPort(String num){
        SharedPreferences.Editor e = share().edit();
        e.putString("SerialPort",num);
        e.apply();
    }

    public static final String KEY_LANGUAGE = "LANGUAGE";
    public static void putString(String key, String value) {
        SharedPreferences.Editor editor = share().edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static String getString(String key) {
        return share().getString(key, "");
    }
}

